package process;

import annocation.BtnListen;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;

/**
 * @author:谢君臣
 * @Date:2021/8/11-20:34
 * @version:1.0
 * @function:
 */
public class ProcessEvent {
    public static void process(Object obj) throws IllegalAccessException, InstantiationException {
        Class cls = obj.getClass();
        Field[] fields = cls.getDeclaredFields();
        for(Field field : fields){
            field.setAccessible(true);
            if (field.isAnnotationPresent(BtnListen.class)) {
                BtnListen annotation = field.getAnnotation(BtnListen.class);
                Class<? extends ActionListener> ActionListener = annotation.value();
                ActionListener actionListener = ActionListener.newInstance();
                AbstractButton abstractButton = (AbstractButton) field.get(obj);
                abstractButton.addActionListener(actionListener);
            }
        }
    }
}
